package com.longcai.piaojia.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.UpdateAppGet;
import com.longcai.piaojia.dialog.UpdateDialog;
import com.longcai.piaojia.fragment.HomeFragment;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.fragment.NewHomeFragment;
import com.longcai.piaojia.fragment.NewsFragment;
import com.longcai.piaojia.fragment.ShowFragment;
import com.longcai.piaojia.fragment.VenueFragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.Http;
import com.zcx.helper.util.UtilApp;

/**
 * Created by maning on 2016/3/14.
 */
public class NavigationActivity extends BaseActivity implements View.OnClickListener {//, AMapLocationListener

    @BoundView(R.id.navigation_home_iv)
    private ImageView navigation_home_iv;
    @BoundView(R.id.navigation_show_iv)
    private ImageView navigation_show_iv;
    @BoundView(R.id.navigation_life_iv)
    private ImageView navigation_life_iv;
    @BoundView(R.id.navigation_venue_iv)
    private ImageView navigation_venue_iv;
    @BoundView(R.id.navigation_mine_iv)
    private ImageView navigation_mine_iv;

    @BoundView(R.id.navigation_home_tv)
    private TextView navigation_home_tv;
    @BoundView(R.id.navigation_show_tv)
    private TextView navigation_show_tv;
    @BoundView(R.id.navigation_life_tv)
    private TextView navigation_life_tv;
    @BoundView(R.id.navigation_venue_tv)
    private TextView navigation_venue_tv;
    @BoundView(R.id.navigation_mine_tv)
    private TextView navigation_mine_tv;

    @BoundView(R.id.navigation_home_rl)
    private RelativeLayout navigation_home_rl;

    private boolean isRefresh = false;

    private HomeFragment homeFragment;

    private ShowFragment showFragment;
    private NewsFragment newsFragment;
    private VenueFragment venueFragment;
    private MineFragment mineFragment;

    private Fragment[] fragments;

    private int index;
    private int currentTabIndex;
    private FragmentTransaction ft;
    private UpdateAppGet updateAppGet = new UpdateAppGet(new AsyCallBack<UpdateAppGet.UpdateAppEntity>() {

        @Override
        public void onSuccess(String toast, int type, final UpdateAppGet.UpdateAppEntity updateAppEntity) throws Exception {
            super.onSuccess(toast, type, updateAppEntity);


            Log.e("-----", "updateAppEntity.constraint" + updateAppEntity.constraint);

            UpdateDialog updateDialog = new UpdateDialog(NavigationActivity.this, getString(R.string.warnning_update_app_text), context.getString(R.string.cancel_text), getString(R.string.update_text)) {
                @Override
                protected void onConfirm() {

                    Uri uri = Uri.parse(updateAppEntity.url);

                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);

                    if (updateAppEntity.constraint == 1) {

                        return;

                    }

                    dismiss();

                }

                @Override
                protected void onCancle() {

                    if (updateAppEntity.constraint == 1) {

                        finish();

                    }

                    dismiss();

                }
            };

            if (updateAppEntity.constraint == 1) {

                updateDialog.setCanceledOnTouchOutside(false);

                updateDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                        finish();

                        return true;
                    }
                });

            }

            updateDialog.show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_navigation);

        Http.getInstance().show(this);

        BaseApplication.BasePreferences.saveCityName("哈尔滨");

        BaseApplication.BasePreferences.saveCityId("96");
        registerReceiver(mHomeKeyEventReceiver, new IntentFilter(
                Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
      //  homeFragment = new HomeFragment();

     //   getSupportFragmentManager().beginTransaction().add(R.id.navigation_content, homeFragment).show(homeFragment).commit();
        setSelect(0);
        updateAppGet.versionNum = UtilApp.versionCode(this);
        updateAppGet.execute(NavigationActivity.this, false);


    }


    @Override
    public void onClick(View view) {

      /*  switch (view.getId()) {

            case R.id.navigation_home_rl:

                index = 0;

                navigation_home_iv.setImageResource(R.mipmap.home_sel);
                navigation_show_iv.setImageResource(R.mipmap.show_no);
                navigation_life_iv.setImageResource(R.mipmap.news_no_sel);
                navigation_venue_iv.setImageResource(R.mipmap.venue_no_sel);
                navigation_mine_iv.setImageResource(R.mipmap.mine_no);

                navigation_home_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));
                navigation_show_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_life_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_venue_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_mine_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));


                //homeFragment = new NewHomeFragment();
                homeFragment = new HomeFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.navigation_content, homeFragment).show(homeFragment).commit();


                break;

            case R.id.navigation_show_rl:

                index = 1;

                navigation_home_iv.setImageResource(R.mipmap.home_no);
                navigation_show_iv.setImageResource(R.mipmap.show_sel);
                navigation_life_iv.setImageResource(R.mipmap.news_no_sel);
                navigation_venue_iv.setImageResource(R.mipmap.venue_no_sel);
                navigation_mine_iv.setImageResource(R.mipmap.mine_no);

                navigation_home_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_show_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));
                navigation_life_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_venue_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_mine_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));


                showFragment = new ShowFragment();

                getSupportFragmentManager().beginTransaction().add(R.id.navigation_content, showFragment).show(showFragment).commit();


                break;

         *//*   case R.id.navigation_life_rl:

                index = 2;

                navigation_home_iv.setImageResource(R.mipmap.home_no);
                navigation_show_iv.setImageResource(R.mipmap.show_no);
                navigation_life_iv.setImageResource(R.mipmap.news_sel);
                navigation_venue_iv.setImageResource(R.mipmap.venue_no_sel);
                navigation_mine_iv.setImageResource(R.mipmap.mine_no);

                navigation_home_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_show_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_life_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));
                navigation_venue_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_mine_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));


                newsFragment = new NewsFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.navigation_content, newsFragment).show(newsFragment).commit();

                break;*//*

            case R.id.navigation_venue_rl:

                index = 3;

                navigation_home_iv.setImageResource(R.mipmap.home_no);
                navigation_show_iv.setImageResource(R.mipmap.show_no);
                navigation_life_iv.setImageResource(R.mipmap.news_no_sel);
                navigation_venue_iv.setImageResource(R.mipmap.venue_sel);
                navigation_mine_iv.setImageResource(R.mipmap.mine_no);

                navigation_home_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_show_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_life_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_venue_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));
                navigation_mine_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));


                venueFragment = new VenueFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.navigation_content, venueFragment).show(venueFragment).commit();

                break;

            case R.id.navigation_mine_rl:

                index = 4;

                navigation_home_iv.setImageResource(R.mipmap.home_no);
                navigation_show_iv.setImageResource(R.mipmap.show_no);
                navigation_life_iv.setImageResource(R.mipmap.news_no_sel);
                navigation_venue_iv.setImageResource(R.mipmap.venue_no_sel);
                navigation_mine_iv.setImageResource(R.mipmap.mine_sel);

                navigation_home_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_show_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_life_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_venue_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
                navigation_mine_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_color));


                mineFragment = new MineFragment();
                getSupportFragmentManager().beginTransaction().add(R.id.navigation_content, mineFragment).show(mineFragment).commit();


                break;

        }*/
        switch (view.getId()) {
            case R.id.navigation_home_rl:
                // vpMain.setCurrentItem(0);
                setSelect(0);
                break;
            case R.id.navigation_show_rl:
                setSelect(1);
                break;

            case R.id.navigation_mine_rl:
                setSelect(2);
                break;

        }

    }


    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        reSetTab();
        hideFragment();
        switch (i) {
            case 0:
                //首页
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    ft.add(R.id.navigation_content, homeFragment);
                }
                navigation_home_iv.setImageResource(R.mipmap.home_sel);
//                // tvHome.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                navigation_home_tv.setTextColor(getResources().getColor(R.color.theme_color));
                ft.show(homeFragment);
                break;
            case 1:
                //投资
                if (showFragment == null) {
                    showFragment = new ShowFragment();
                    ft.add(R.id.navigation_content, showFragment);
                }
                navigation_show_iv.setImageResource(R.mipmap.show_sel);
//                //  tvTouzi.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                navigation_show_tv.setTextColor(getResources().getColor(R.color.theme_color));
                ft.show(showFragment);
                break;
            case 2:
                //资产
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    ft.add(R.id.navigation_content, mineFragment);
                }
                navigation_mine_iv.setImageResource(R.mipmap.mine_sel);
//                tv_main_tab_04.setTextColor(getResources().getColor(R.color.colorMainBlue));
                navigation_mine_tv.setTextColor(getResources().getColor(R.color.theme_color));
                ft.show(mineFragment);
                break;

        }
        ft.commit();
    }


    private void reSetTab() {
        navigation_home_iv.setImageResource(R.mipmap.home_no);
        navigation_show_iv.setImageResource(R.mipmap.show_no);
        navigation_life_iv.setImageResource(R.mipmap.news_no_sel);
        navigation_venue_iv.setImageResource(R.mipmap.venue_no_sel);
        navigation_mine_iv.setImageResource(R.mipmap.mine_no);

        navigation_home_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
        navigation_show_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
        navigation_life_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
        navigation_venue_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
        navigation_mine_tv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.text_color));
    }


    private void hideFragment() {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (showFragment != null) {
            ft.hide(showFragment);
        }

        if (mineFragment != null) {
            ft.hide(mineFragment);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();


        if (isRefresh) {

            isRefresh = false;

            switch (index) {

                case 0:

                    Log.v("maning", "home进入前台");

                    try {

                        NewHomeFragment.RefreshNewHome refreshNewHome = (NewHomeFragment.RefreshNewHome) getAppCallBack(NewHomeFragment.class);
                      //  refreshNewHome.refresh();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;

                case 1:

                    Log.v("maning", "show进入前台");

                    try {

                        ShowFragment.setCityNameIdCallback setCityNameIdCallback = (ShowFragment.setCityNameIdCallback) getAppCallBack(ShowFragment.class);

                        setCityNameIdCallback.refresh();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    break;

                case 4:

                    Log.v("maning", "mine进入前台");

                    try {

                        MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);
                        getUserAdvert.refreshInfo();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    break;

            }

        }


    }

    private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {
        String SYSTEM_REASON = "reason";
        String SYSTEM_HOME_KEY = "homekey";
        String SYSTEM_HOME_KEY_LONG = "recentapps";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_REASON);
                if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
                    //表示按了home键,程序到了后台
                    Log.v("maning", "进入后台");

                    isRefresh = true;

                } else if (TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)) {
                    //表示长按home键,显示最近使用的程序列表
                }
            }
        }
    };

}
